﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _4DJackPot
{
    public partial class Login : Form
    {
        public bool isLogin;
        public Login()
        {
            InitializeComponent();

            isLogin = false;

        }

        private void btn_login_Click(object sender, EventArgs e)
        {
            if(this.tB_userId.Text == "")
            {
                MessageBox.Show("Input USER ID.", "Help", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tB_userId.Focus();
                return;
            }
            if (this.tB_UserPw.Text == "")
            {
                MessageBox.Show("Input USER PW.", "Help", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tB_UserPw.Focus();
                return;
            }
            /*
            if (this.tB_userId.Text == "USER" && this.tB_UserPw.Text == "USER")
            {
                Menu menu = new Menu();
                menu.Show();
                this.Close();
                
            }
            
            if (this.tB_userId.Text == "ADMIN" && this.tB_UserPw.Text == "ADMIN")
            {
                AdminMenu menu = new AdminMenu();
                menu.Show();
                this.Close();

            }
            else
            {
                MessageBox.Show("Username and password is incorrect.", "Help", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tB_userId.Focus();
                return;
            }
            */
            string idString = this.tB_userId.Text ?? "";
            string pwString = this.tB_UserPw.Text ?? "";

            //select query 작성
            string selectQueryString =
                "SELECT DISTINCT name, permission_id FROM users " +
                "WHERE user_id = '" + idString + "'" + " AND " +
                "password = '" + pwString + "'";
            
            DbConn DbConn = new DbConn();

            // DbConn에 ReadQuery 호출 하면 해당 쿼리가 실행되어 DataSet으로 Return됨
            DataSet resultReadDataSet = DbConn.ReadQuery(selectQueryString);

            if (resultReadDataSet == null)
            {
                MessageBox.Show("Can`t connect to DB. Please check the DB.", "Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else if (resultReadDataSet.Tables[0].Rows.Count == 0)
            {
                MessageBox.Show("User ID is not present or does not match the password.", "Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                isLogin = true;
                Global.idString = idString;
                Global.pwString = pwString;
                // Db에서 가져온 DataSet 활용 
                // select 된 row가 다수일 경우 forreach문 사용 아래 참고

                //string selectQueryString =
                //    "SELECT BASIC_ITEM_CODE, BASIC_NAME FROM BASIC_TB WHERE BASIC_TYPE = '정량' ORDER BY BASIC_ITEM_CODE";
                //SPCDbConn sPCDbConn = new SPCDbConn();
                //DataSet resultReadDataSet = sPCDbConn.ReadQuery(selectQueryString);
                //foreach (DataRow row in resultReadDataSet.Tables[0].Rows)
                //{
                //    this.cB_identify.Items.Add(row["BASIC_NAME"].ToString());
                //}
                
                Global.nameString = resultReadDataSet.Tables[0].Rows[0]["name"].ToString() ?? "";
                Global.permissionString = resultReadDataSet.Tables[0].Rows[0]["permission_id"].ToString() ?? "";

                /*
                this.Hide();
                MainForm menu = new MainForm();
                menu.Show();
                this.Close();
                */
                this.DialogResult = DialogResult.OK;

                this.Close();
                
            }

        }

        private void bTn_Exit_Click(object sender, EventArgs e)
        {
            try
            {
                Application.Exit();
                Environment.Exit(0);
            }
            catch (Exception e1)
            {
                FileLog FileLog = new FileLog();
                FileLog.WriteErrorLog(e1.StackTrace + "\n" + e1.Message);
            }
        }

        private void btn_join_Click(object sender, EventArgs e)
        {
            try
            {
                Join join = new Join();
                join.ShowDialog();
            }
            catch (Exception e1)
            {
                FileLog FileLog = new FileLog();
                FileLog.WriteErrorLog(e1.StackTrace + "\n" + e1.Message);
            }
        }

        private void Main_Load(object sender, EventArgs e)
        {
            DbConn DbConn = new DbConn();
            if (!DbConn.DbConnect(true))
            {
                Ini Ini = new Ini();

                Ini.WriteIni("MACRO", "KINDDB", "0");
                Ini.WriteIni("MACRO", "DBPATH", Application.StartupPath + "\\Data\\MDB\\TEST.mdb");
                Global.dbType = 0;
                Global.dbFileString = Application.StartupPath + "\\Data\\MDB\\TEST.mdb";
            }
        }

        private void bTn_dbSetting_Click(object sender, EventArgs e)
        {
            try
            {
                DbSetting dbSetting = new DbSetting();
                dbSetting.ShowDialog();
            }
            catch (Exception e1)
            {
                FileLog FileLog = new FileLog();
                FileLog.WriteErrorLog(e1.StackTrace + "\n" + e1.Message);
            }
        }

        private void tB_UserPw_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar == 13)
            {
                btn_login_Click(null, null);
            }
        }

        private void tB_userId_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                btn_login_Click(null, null);
            }
        }

        private void Login_FormClosing(object sender, FormClosingEventArgs e)
        {
            
            if (isLogin == false && DialogResult.Yes ==
                    MessageBox.Show("Do you want to exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                Application.ExitThread();
                Environment.Exit(0);
            }
            else if(isLogin == true)
            {


            }else
            {
                e.Cancel = true;
            }
        }




    }
}
